package cn.iocoder.yudao.module.blog.controller.app.jiucase;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.blog.controller.admin.casecomment.vo.BlogCaseCommentCreateReqVO;
import cn.iocoder.yudao.module.blog.controller.admin.casecomment.vo.BlogCaseCommentRespVO;
import cn.iocoder.yudao.module.blog.controller.admin.postcomment.vo.BlogPostCommentCreateReqVO;
import cn.iocoder.yudao.module.blog.controller.admin.postcomment.vo.BlogPostCommentRespVO;
import cn.iocoder.yudao.module.blog.controller.app.jiucase.vo.commentvo.AppBlogCaseCommentPageReqVO;
import cn.iocoder.yudao.module.blog.controller.app.jiucase.vo.commentvo.AppBlogCaseCommentRespVO;
import cn.iocoder.yudao.module.blog.controller.app.post.commentvo.AppBlogPostCommentPageReqVO;
import cn.iocoder.yudao.module.blog.controller.app.post.commentvo.AppBlogPostCommentRespVO;
import cn.iocoder.yudao.module.blog.convert.casecomment.BlogCaseCommentConvert;
import cn.iocoder.yudao.module.blog.convert.postcomment.BlogPostCommentConvert;
import cn.iocoder.yudao.module.blog.dal.dataobject.casecomment.BlogCaseCommentDO;
import cn.iocoder.yudao.module.blog.dal.dataobject.postcomment.BlogPostCommentDO;
import cn.iocoder.yudao.module.blog.service.casecomment.AppBlogCaseCommentService;
import cn.iocoder.yudao.module.blog.service.casecomment.BlogCaseCommentService;
import cn.iocoder.yudao.module.blog.service.jiucase.BlogJiuCaseService;
import cn.iocoder.yudao.module.blog.service.post.BlogPostService;
import cn.iocoder.yudao.module.blog.service.postcomment.AppBlogPostCommentService;
import cn.iocoder.yudao.module.blog.service.postcomment.BlogPostCommentService;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

@Tag(name = "用户 APP - 案例评论")
@RestController
@RequestMapping("/blog/case/comment")
@Validated
public class AppBlogJiuCaseCommentController {

    @Resource
    private BlogCaseCommentService blogCaseCommentService;

    @Resource
    private BlogJiuCaseService blogJiuCaseService;

    @Resource
    private AppBlogCaseCommentService appBlogCaseCommentService;

    @Resource
    private MemberUserApi memberUserApi;

    @PostMapping("/create")
    @Operation(summary = "创建评论")
    public CommonResult<AppBlogCaseCommentRespVO> createComment(@Valid @RequestBody BlogCaseCommentCreateReqVO createReqVO) {
        Long id = blogCaseCommentService.createCaseComment(createReqVO);
        blogJiuCaseService.updateCaseCommentNum(createReqVO.getCaseId(), 1);

        BlogCaseCommentDO blogCaseCommentDO = blogCaseCommentService.getCaseComment(id);
        MemberUserRespDTO memberUserRespDTO = memberUserApi.getUser(blogCaseCommentDO.getMemberId());
        return success(BlogCaseCommentConvert.INSTANCE.convertApp(blogCaseCommentService.getCaseComment(id), memberUserRespDTO));
    }

    @GetMapping("/child")
    @Operation(summary = "获得帖子评论")
    @Parameter(name = "commentId", description = "编号", required = true, example = "1024")
    public CommonResult<Long> childCommentCount(@RequestParam("commentId") Long commentId) {
        return success(blogCaseCommentService.getChildCount(commentId));
    }

    @GetMapping("/page")
    @Operation(summary = "获得帖子评论分页")
    public CommonResult<PageResult<AppBlogCaseCommentRespVO>> getCommentPage(@Valid AppBlogCaseCommentPageReqVO pageVO) {
        PageResult<BlogCaseCommentDO> pageResult = appBlogCaseCommentService.getCaseCommentPage(pageVO);

        // 查询用户信息
        Set<Long> memberIds = convertSet(pageResult.getList(), BlogCaseCommentDO::getMemberId);
        Map<Long, MemberUserRespDTO> memberMap = memberUserApi.getUserMap(memberIds);

        return success(BlogCaseCommentConvert.INSTANCE.convertAppPage(pageResult, memberMap));
    }

    @GetMapping("/totalcount")
    @Operation(summary = "获得案例总评论数量")
    @Parameter(name = "caseId", description = "案例编号", required = true, example = "1024")
    public CommonResult<Long> getTotalCount(@RequestParam("caseId") Long caseId){
        return success(appBlogCaseCommentService.getTotalCount(caseId));
    }
}
